package com.yueyang.sojourn.server.job;

import com.yueyang.sojourn.server.model.enums.ShelfStatusEnum;
import com.yueyang.sojourn.server.model.po.SojournMerch;
import com.yueyang.sojourn.server.service.sojourn.merch.SojournMerchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yemingxing
 * @summary 商品下架定时任务
 * @Copyright (c) 2020, Yueyang Group All Rights Reserved.
 * @since 2020-02-20 17:08
 */
@Component
@Slf4j
public class MerchJob {

    @Autowired
    private SojournMerchService sojournMerchService;

    /**
     * 商品过期自动下架
     */
    @Scheduled(cron = "0 10 0 * * ?")
    public void merchOff() {
        log.info("【JOB】start: 商品过期自动下架");
        Date nowDate = new Date();
        List<SojournMerch> merchList = sojournMerchService.selectByShelfStatus(ShelfStatusEnum.BEING_ON.getCode());
        List<Long> merchIds = merchList.stream()
                .filter(merch -> merch.getAvailableDateEnd().before(nowDate))
                .map(SojournMerch::getId)
                .collect(Collectors.toList());
        sojournMerchService.updateShelfStatusByIds(ShelfStatusEnum.OFF.getCode(), merchIds);
        log.info("【JOB】end: 商品过期自动下架");
    }
}
